package com.nnnu.wsnackshop.controller.admin;


import com.nnnu.wsnackshop.constant.MessageConstants;
import com.nnnu.wsnackshop.pojo.dto.AddStoreAddressDTO;
import com.nnnu.wsnackshop.pojo.dto.StoreAddressPageDTO;
import com.nnnu.wsnackshop.pojo.dto.UpdateStoreAddressDTO;
import com.nnnu.wsnackshop.pojo.vo.StoreAddressVO;
import com.nnnu.wsnackshop.result.Result;
import com.nnnu.wsnackshop.service.IStoreAddressesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 门店地址表 前端控制器
 * </p>
 *
 * @author zk
 * @since 2025-05-19
 */
@RestController("adminStoreAddressesController")
@RequestMapping("/api/admin/store-addresses")
@Slf4j
@Validated
@Tag(name = "管理端-门店地址接口")
@RequiredArgsConstructor
public class StoreAddressesController {

    private final IStoreAddressesService addressService;

    @Operation(summary = "分页查询门店地址", description = "仅 superAdmin 或 admin 且拥有 storeaddress:read 权限可访问")
    @GetMapping("/page")
    @RequiresRoles(value = {"superAdmin", "admin"}, logical = Logical.OR)
    @RequiresPermissions("storeaddress:read")
    public Result<?> pageAddresses(
            @Valid @ParameterObject StoreAddressPageDTO dto) {
        log.info("分页查询门店地址：{}", dto);
        return Result.success(addressService.pageAddresses(dto), MessageConstants.SELECT_OK);
    }

    @Operation(summary = "查询指定门店所有地址", description = "仅 superAdmin 或 admin 且拥有 storeaddress:read 权限可访问")
    @GetMapping("/list/{storeId}")
    @RequiresRoles(value = {"superAdmin", "admin"}, logical = Logical.OR)
    @RequiresPermissions("storeaddress:read")
    public Result<List<StoreAddressVO>> listByStore(
            @Parameter(description = "门店ID", in = ParameterIn.PATH)
            @PathVariable Long storeId) {
        log.info("查询门店({})地址列表", storeId);
        List<StoreAddressVO> list = addressService.listByStore(storeId);
        return Result.success(list, MessageConstants.SELECT_OK);
    }

    @Operation(summary = "新增门店地址", description = "仅 superAdmin 且拥有 storeaddress:create 权限可访问")
    @PostMapping
    @RequiresRoles("superAdmin")
    @RequiresPermissions("storeaddress:create")
    public Result<Void> createAddress(
            @Valid @RequestBody @Parameter(description = "新增门店地址 DTO") AddStoreAddressDTO dto) {
        log.info("新增门店地址：{}", dto);
        addressService.createAddress(dto);
        return Result.success(MessageConstants.ADD_OK);
    }

    @Operation(summary = "更新门店地址", description = "仅 superAdmin 且拥有 storeaddress:update 权限可访问")
    @PutMapping
    @RequiresRoles("superAdmin")
    @RequiresPermissions("storeaddress:update")
    public Result<Void> updateAddress(
            @Valid @RequestBody @Parameter(description = "更新门店地址 DTO") UpdateStoreAddressDTO dto) {
        log.info("更新门店地址：{}", dto);
        addressService.updateAddress(dto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    @Operation(summary = "批量删除门店地址", description = "仅 superAdmin 且拥有 storeaddress:delete 权限可访问")
    @DeleteMapping
    @RequiresRoles("superAdmin")
    @RequiresPermissions("storeaddress:delete")
    public Result<Void> deleteAddresses(
            @RequestParam @Parameter(description = "要删除的ID列表", in = ParameterIn.QUERY) List<@Min(1) Integer> ids) {
        log.info("批量删除门店地址：{}", ids);
        addressService.deleteAddresses(ids);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }
}
